/**
 * @file chassis.h
 * @brief 底盘控制模块头文件
 *        定义底盘模式枚举、结构体类型、控制宏及电机数据发送接口
 * @author WeiFan Qiu (qiuweifan0523@faxmail.com)
 * @version 1.0
 * @date 2025-08-07
 * 
 * @copyright Copyright (c) 2025  邱伟凡
 * 
 * @par 修改日志:
 *	 2025-08-27: 删除半舵相关宏定义，完善无状态反馈的电机数据发送
 * @bug 待解决问题:
 */
#ifndef CHASSIS_H
#define CHASSIS_H
#include "QWF_Custom_Ctrl.h"	//专控文件
#include "QWF_Include.h"
#include "QWF_Cmd.h"
#include "stdint.h"
#include "motor.h"
#include "state.h"
#include "HI229.h"
#include "pid.h"
#define wheel_v_max				((5000+500*judge->robot_status.robot_level+400)>=9000?9000:(5000+500*judge->robot_status.robot_level+400))	
#define chassis_normal_v_max	wheel_v_max*0.7f
#define chassis_fly_v_max		8000
typedef enum{//底盘的模式
	chassis_stop=0,		//停止
	chassis_yes,		//正常
	chassis_normal,		//正常
	chassis_follow,		//底盘跟随
	chassis_top,		//小陀螺
	chassis_fly_slope,	//飞坡
	chassis_mode_num	//模式数量
}chassis_mode_e;
typedef void(*chassis_ctrl_func_t)(void);
#ifndef Chassis_Wheel_Type
	#error "Undefine 'Chassis_Wheel_Type'"  // 未定义底盘类型时报错
#elif ((Chassis_Wheel_Type<Chassis_Wheel_Type_Min)||(Chassis_Wheel_Type>Chassis_Wheel_Type_Max))
	#error "Error state of Chassis_Wheel_Type'"  // 底盘类型非法时报错
#endif
typedef struct{
	chassis_mode_e mode[2];//底盘模式	
	struct{//存放局部的 part
		struct{//与云台偏差角
			float target;
			DJI_Motor_t motor;
		}yaw;
		int8_t signed_all;//用于一键反转			
		#if Chassis_Wheel_Type==Helf_Helm_Wheel
		struct{
			struct{
				struct{
					float angle;
					float 	w;
					int16_t out;
				}target;
				DJI_Motor_t motor;
			}helm;
			struct{	
				struct{
					int8_t signed_v;//前轮c620反转
					float 	v;
					int16_t out;
				}target;
				DJI_Motor_t motor;				
			}wheel;
		}f;
		struct{
			struct{
				struct{
					float angle;
					float 	w;
					int16_t out;
				}target;
				DJI_Motor_t motor;				
			}helm;
			struct{	
				struct{
					int8_t signed_v;//后轮c620反转
					float 	v;
					int16_t out;
				}target;
				DJI_Motor_t motor;					
			}wheel;
		}b;
		struct{
			struct{	
				struct{
					float 	v;
					int16_t out;
				}target;
				DJI_Motor_t motor;					
			}wheel;				
		}l;
		struct{
			struct{	
				struct{
					float 	v;
					int16_t out;
				}target;
				DJI_Motor_t motor;				
			}wheel;		
		}r;				
		#elif Chassis_Wheel_Type==Mecanum_Wheel||Chassis_Wheel_Type==Omni_Wheel
		struct{
			struct{ 
				float v;
				int16_t out;
			}target;
			DJI_Motor_t motor;				
		}lf;
		struct{
			struct{ 
				float v;
				int16_t out;
			}target;
			DJI_Motor_t motor;	
		}rf;
		struct{
			struct{ 
				float v;
				int16_t out;
			}target;
			DJI_Motor_t motor;	
		}lb;
		struct{
			struct{ 
				float v;
				int16_t out;
			}target;
			DJI_Motor_t motor;	
		}rb;
		#elif Chassis_Wheel_Type==Leg_Wheel
		
		#endif
	}part;//轮子速度
	struct{//存放整体的（整车运动状态）
		struct{
			struct{
				float target[2];	
			}x;
			struct{
				float target[2];
			}y;
			struct{
				float target[2];
			}w;
			struct{
				float target[2];
			}all;
			struct{
				float target[2];
			}angle;
		}v;
		struct{
			float w;
			float w_now;
			_Bool use_top_change;
		}top;
	}sys;//整车运动状态（包含速度/角速度目标）
	const chassis_ctrl_func_t ctrl_func[chassis_mode_num];//底盘控制函数
	chassis_pid_t* pid;
}chassis_t;
extern chassis_t* chassis;
#ifdef STATE_H
#define send_data_chassis_gm6020(motor_where)/*全都用小写*/\
do{\
	switch(qwf_state->chassis.motor.motor_where.state){\
		case gm6020_ok:	break;\
		case gm6020_lose:		chassis->part.motor_where.target.out=0;break;\
		case gm6020_too_hot_1:	chassis->part.motor_where.target.out=0;break;\
		case gm6020_too_hot_2:	chassis->part.motor_where.target.out=0;break;\
		case gm6020_i_over_max:	chassis->part.motor_where.target.out=0;break;\
	}\
	chassis->part.motor_where.motor.config.tx_adress[0]=(chassis->part.motor_where.target.out>>8);\
	chassis->part.motor_where.motor.config.tx_adress[1]=(chassis->part.motor_where.target.out);	\
}while(0);
#define send_data_chassis_c620(motor_where)/*全都用小写*/\
do{\
	switch(qwf_state->chassis.motor.motor_where.state){\
		case c620_ok:break;\
		case c620_lose:			chassis->part.motor_where.target.out=0;break;\
		case c620_i_over_max:	chassis->part.motor_where.target.out=0;break;\
	}\
	chassis->part.motor_where.motor.config.tx_adress[0]=(chassis->part.motor_where.target.out>>8);\
	chassis->part.motor_where.motor.config.tx_adress[1]=(chassis->part.motor_where.target.out);	\
}while(0);
#else
#define send_data_chassis_gm6020(motor_where)/*全都用小写*/\
do{\
	chassis->part.motor_where.motor.config.tx_adress[0]=(chassis->part.motor_where.target.out>>8);\
	chassis->part.motor_where.motor.config.tx_adress[1]=(chassis->part.motor_where.target.out);	\
}while(0);
#define send_data_chassis_c620(motor_where)/*全都用小写*/\
do{\
	chassis->part.motor_where.motor.config.tx_adress[0]=(chassis->part.motor_where.target.out>>8);\
	chassis->part.motor_where.motor.config.tx_adress[1]=(chassis->part.motor_where.target.out);	\
}while(0);
#endif
#endif
